#include <QDir>
#include <QInputDialog>
#include <QListWidgetItem>
#include <QMessageBox>

#include "hbot.h"
#include "profilemanager.h"
#include "profileManagereditprofile.h"
#include "sdefs.h"
#include "xmlconfig.h"

CProfileManager::CProfileManager(QWidget *parent): QDialog(parent), Ui::ProfileManager()
{
	setupUi(this);

	connect(pushButtonCreateProfile, SIGNAL(clicked()), this, SLOT(ActionCreateProfile()));
	connect(pushButtonEditProfile, SIGNAL(clicked()), this, SLOT(ActionEditProfile()));
	connect(pushButtonDeleteProfile, SIGNAL(clicked()), this, SLOT(ActionDeleteProfile()));
	connect(listProfileManager, SIGNAL(itemPressed(QListWidgetItem *)), this, SLOT(ActionItemClicked(QListWidgetItem *)));
	connect(listProfileManager, SIGNAL(itemDoubleClicked(QListWidgetItem *)), this, SLOT(ActionEditProfile(QListWidgetItem *)));
	
	if (g_HBot.GetXMLProfile()->GetErrorXML())
	{
		m_XMLHasError = true;
		labelError->setText("<font color=#FF0000>XML Error: " + g_HBot.GetXMLProfile()->GetErrorString() + "<br>Unable to list profiles until error has been resolved.</font>");
	}
	else
	{
		m_XMLHasError = false;
		labelError->setText(QString::null);
		listProfileManager->addItems(g_HBot.GetXMLProfile()->GetProfiles());
	}
}

CProfileManager::~CProfileManager()
{
	
}

void CProfileManager::ActionCreateProfile()
{
	if (!m_XMLHasError)
	{
		bool ok;
		bool itemExists = false;
		bool invalid = false;
		QString profile;

		do
		{
			if (itemExists)
			{
				profile = QInputDialog::getText(this, tr("Create Profile"), tr("A profile with that name already exists. Try a different name.\nProfile Name:"), QLineEdit::Normal, QString(), &ok);
			}
			else if (invalid)
			{
				profile = QInputDialog::getText(this, tr("Create Profile"), tr("The profile name must not contain: \\ / : * ? \" < > |\n. Try a different name.\nProfile Name:"), QLineEdit::Normal, QString(), &ok);
			}
			else
			{
				profile = QInputDialog::getText(this, tr("Create Profile"), tr("Profile Name:"), QLineEdit::Normal, QString(), &ok);
			}

			if (!ok)
			{
				return;
			}

			if (!listProfileManager->findItems(profile, Qt::MatchFixedString).empty())
			{
				itemExists = true;
			}
			else
			{
				itemExists = false;
			}

			if (g_HBot.ContainsInvalidCharacters(profile))
			{
				invalid = true;
			}
			else
			{
				invalid = false;
			}
		} while (profile.isEmpty() || itemExists || invalid);

		g_HBot.CreateProfileDirectory(profile);
		g_HBot.GetXMLProfile()->CreateProfile(profile);
		listProfileManager->addItem(profile);
		listProfileManager->setCurrentItem(listProfileManager->findItems(profile, Qt::MatchFixedString)[0]);
		ActionItemClicked(listProfileManager->findItems(profile, Qt::MatchFixedString)[0]);

		if (g_HBot.GetActionEmptyProfileList())
		{
			g_HBot.GetMenuSelectProfile()->removeAction(g_HBot.GetActionEmptyProfileList());
			delete g_HBot.GetActionEmptyProfileList();
			g_HBot.SetActionEmptyProfileList(NULL);
		}

		QAction *temp = g_HBot.GetMenuSelectProfile()->addAction(profile);
		connect(temp, SIGNAL(triggered()), g_HBot.GetPtr(), SLOT(ActionSelectProfile()));
	}
	else
	{
		g_HBot.GetXMLProfile()->ErrorCheck();
	}
}

void CProfileManager::ActionDeleteProfile()
{
	if (!m_XMLHasError)
	{
		QListWidgetItem *item = listProfileManager->currentItem();
		QList<QAction *> profiles;

		switch (QMessageBox::question(g_HBot.GetPtr(), QObject::tr("Delete Profile"), QObject::tr("Are you sure you want to delete profile: <b>%1</b><br>Any settings or data related to the profile will be removed.").arg(item->text()), QMessageBox::Yes, QMessageBox::No))
		{
		case QMessageBox::Yes:
			g_HBot.DeleteDirectory(QDir::currentPath() + "/Data/Profiles/" + item->text());
			g_HBot.GetXMLProfile()->DeleteProfile(item->text());
			listProfileManager->takeItem(listProfileManager->row(item));

			if (!listProfileManager->count())
			{
				pushButtonEditProfile->setEnabled(false);
				pushButtonDeleteProfile->setEnabled(false);
			}

			profiles = g_HBot.GetMenuSelectProfile()->actions();

			for (int i = 0; i < profiles.size(); ++i)
			{
				if (profiles[i]->text() == item->text())
				{
					g_HBot.GetMenuSelectProfile()->removeAction(profiles[i]);

					if (!(profiles.size() - 1) && !(g_HBot.GetActionEmptyProfileList()))
					{
						g_HBot.SetActionEmptyProfileList(g_HBot.GetMenuSelectProfile()->addAction(MENU_SELECT_ACTION_EMPTYPROFILELIST));
					}

					break;
				}
			}


			if (g_HBot.GetXMLProfile()->GetDefaultProfile() == item->text())
			{
				g_HBot.GetXMLProfile()->SetDefaultProfile(QString::null);
			}

			return;

		case QMessageBox::No:
			return;
		}
	}
	else
	{
		g_HBot.GetXMLProfile()->ErrorCheck();
	}
}

void CProfileManager::ActionEditProfile(QListWidgetItem *)
{
	CProfileManagerEditProfile profileManagerEditProfile(listProfileManager->currentItem()->text(), this);
	profileManagerEditProfile.exec();
}

void CProfileManager::ActionItemClicked(QListWidgetItem *)
{
	pushButtonEditProfile->setEnabled(true);
	pushButtonDeleteProfile->setEnabled(true);
}
